% TEST_ID: LANG-EXPAND-0072
% CATEGORY: stdlib/arithmetic_ops/addition
% 测试 sum 函数

function test_sum
    A = 1:10;
    S = sum(A);
    assert(isequal(S, 55));


    A = [1 3 2; 4 2 5; 6 1 4];
    S = sum(A);

    assert(isequal(S, [11 6 11]));

    A = [1 3 2; 4 2 5; 6 1 4];

    S = sum(A, 2);

    assert(isequal(S, [6; 11; 11]));

    A = ones(4, 3, 2);

    S1 = sum(A, [1, 2]);

    assert(isequal(size(S1), [1 1 2]));

    assert(S1(:, :, 1) == 12);
    assert(S1(:, :, 2) == 12);

    S2 = sum(A, [2 3]);

    assert(isequal(S2, [6; 6; 6; 6]));

    S3 = sum(A, [1 3]);

    assert(isequal(S3, [8 8 8]));

    S4 = sum(A, [1 2 3]);

    assert(S4 == 24);

    Sall = sum(A, 'all');

    assert(Sall == 24);

    A = ones(4, 2, 3);
    S = sum(A, 3);

    assert(isequal(S, [3 3; 3 3; 3 3; 3 3]));

    A = [1.77 -0.005 3.98 -2.95 NaN 0.34 NaN 0.19];
    S = sum(A, 'omitnan');

    assert(abs(S - 3.3250) < 1e-15);

    A = [1.77 -0.005 3.98 -2.95 NaN 0.34 NaN 0.19];
    S = sum(A);

    assert(isnan(S));

    A = [
        0.1891 0.2819 0.4991
        0.0427 0.5386 0.5358
        0.6352 0.6952 0.4452
    ];

    assert(isequal(sum(int64(round(A * 100))), [87 152 149]));
    assert(isequal(sum(int64(round(A * 100)), 'native'), int64([87 152 149])));
    assert(isequal(class(sum(int64(round(A * 100)), 'native')), 'int64'));

    assert(sum([]) == 0);

    A = uint64([256 1024 268435456 137438953472 8796093022208 70368744177664 1125899906842624 9223372036854775808]);
    assert(isequal(sum(A), 0x8004482010000800));

end
